package generadorDescriptores;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import generadorDescriptores.database.ConnectionManager;
import generadorDescriptores.dom.XmlDocument;
import generadorDescriptores.dom.XmlFile;
import generadorDescriptores.forms.edition.XMLPanel;

import org.w3c.dom.Document;

import com.project.frba.bi.BiUtil;
import com.project.frba.bi.descriptor.DatabaseDescriptor;
import com.project.frba.bi.descriptor.Dimension;
import com.project.frba.bi.descriptor.Join;
import com.project.frba.bi.descriptor.JoinCondition;
import com.project.frba.bi.descriptor.Property;

public class FileInEdition {

	String directory;
	String filename;
	String fullName;
	int index;
	DatabaseDescriptor descriptor;
	XmlDocument xmldocument;
	ConnectionManager connectionManager;
	String database;
	
	
	public void load(){
		this.directory = ".\\descriptors\\";
		this.filename = Fachada.getFileName(fullName);
		this.descriptor = new DatabaseDescriptor(this.xmldocument.getDocument());
		descriptor.load();
	}
	
	public void setDatabase(String db){
		this.database = db;
	}
	
	public String getDatabase(){
		return this.database;
	}
	
	public void setXMLDocument(XmlDocument xmldoc){
		this.xmldocument = xmldoc;
	}
	
	public XmlDocument getXmlDocument(){
		return xmldocument;
	}
	
	public String getDirectory() {
		return directory;
	}
	
	public void setDirectory(String directory) {
		this.directory = directory;
	}
	
	public String getFilename() {
		return filename;
	}
	
	public void setFilename(String filename) {
		this.filename = filename;
	}
	
	public String getFullName() {
		return fullName;
	}
	
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
	
	public int getIndex() {
		return index;
	}
	
	public void setIndex(int index) {
		this.index = index;
	}
	
	public DatabaseDescriptor getDescriptor(){
		return this.descriptor;
	}
	
	public void setConnectionManager(ConnectionManager connection){
		this.connectionManager = connection;
	}
	
	public ConnectionManager getConnectionManager(){
		return this.connectionManager;
	}
	
	public void refreshDocument(){
		this.xmldocument = new XmlDocument();
		this.xmldocument.setDocument(this.convertToDocument());
	}
	
	
	public Document convertToDocument(){
		XmlDocument xml = new XmlDocument();
		AttributesData data = new AttributesData();
		data.setBiName(descriptor.getDescriptorName().toLowerCase());
		data.setBiLabel(descriptor.getLabel());
		data.setBiPrivilege(descriptor.getPrivilege());
		data.setFacttableTable(descriptor.getFacttable());
		data.setMetric(descriptor.getMetric().toLowerCase());
		data.setFormatter(descriptor.getFormatterClassName());
		if(descriptor.getCurrencyType()!=null){
			data.setCurrencyType(descriptor.getCurrencyType().toString());
		}			
		Class metricType = descriptor.getMetricType();
		String typeAsString = BiUtil.typeClassToString(metricType);
		data.setFacttableType(typeAsString.toLowerCase());
		xml.buildDescriptorAndFacttable(data);
		
		xml.addTagJoins();
		Collection<Join> joins = descriptor.getJoins();
		for (Iterator iterator = joins.iterator(); iterator.hasNext();) {
			Join join = (Join) iterator.next();
			//AttributesData data2 = new AttributesData();
			data.setJoinName(join.getName().toLowerCase());
			data.setJoinTable(join.getTable());
			ArrayList<JoinCondition>conditions = join.getJoinConditions();
			for (int i = 0; i < conditions.size(); i++) {
				data.setConditionJoinColumn(conditions.get(i).getColumn());
				data.setConditionJoinFactColumn(conditions.get(i).getFactColumn());
				xml.addConditionToJoin(join.getName().toLowerCase(), data);
			}			
		}
		
		xml.addTagDimensions();
		Collection<Dimension>dimensions = descriptor.getDimensions();
		for (Iterator iterator = dimensions.iterator(); iterator.hasNext();) {
			Dimension dim = (Dimension) iterator.next();
			data.setDimensionName(dim.getName().toLowerCase());
			Collection<Property> props = dim.getProperties();
			for (Iterator iterator2 = props.iterator(); iterator2.hasNext();) {
				Property prop = (Property) iterator2.next();
				data.setPropName(prop.getName().toLowerCase());
				data.setPropLabel(prop.getLabel());
				data.setPropColumn(prop.getColumn());
				data.setPropTable(prop.getTable());
				data.setPropDefault(prop.getDefaultValue());
				if(prop.getFunction()!=null){
					data.setPropFunction(prop.getFunction().toLowerCase());
				}
				if(prop.getType()!= null && !prop.getType().equals("")){
					data.setPropType(BiUtil.typeClassToString(prop.getType()));
				}
				else
					data.setPropType(null);
				
				
				
				xml.addPropertyToDimension(data, dim.getName().toLowerCase());
			}
		}
		return xml.getDocument();
	}
	
	public void removeConditionFromJoin(String joinName, String conditionName){
		Collection<Join> joins = getDescriptor().getJoins();
		for (Iterator iterator = joins.iterator(); iterator.hasNext();) {
			Join join = (Join) iterator.next();
			if(join.getName().equals(joinName)){
				ArrayList<JoinCondition> conditions = join.getJoinConditions(); 
				for (int i = 0; i < conditions.size(); i++) {
					if(conditionName.equals(conditions.get(i).toString())){
						join.removeJoinCondition(conditions.get(i));
					}
				}
			}
		}
	}
	
	public Collection<Join> getJoins(){
		return descriptor.getJoins();
	}
	
	public void addConditionToJoin(String joinName, JoinCondition joinCondition){
		Collection<Join> joins = descriptor.getJoins();
		for (Iterator iterator = joins.iterator(); iterator.hasNext();) {
			Join join = (Join) iterator.next();
			if(join.getName().equals(joinName)){
				join.addJoinCondition(joinCondition);
			}
		}
	}
	
	public void setMetric(String metric){
		this.descriptor.setMetric(metric);
	}
	
	public void setMetricType(String type){
		this.descriptor.setMetricType(BiUtil.getMetricType(type));
	}
	
	public void setFormatter(String formatter){
		descriptor.setFormatterClassName(formatter);
	}
	
	public void setCurrencyType(int type){
		descriptor.setCurrencyType(type);
	}
	
	public void reWrite(){
		Document document = this.convertToDocument();
		String text = XmlDocument.DocumentToString(document);
		try {
			//FileOutputStream fout = new FileOutputStream(".\\descriptors\\"+filename);
			FileOutputStream fout = new FileOutputStream(this.fullName);
			PrintWriter pout = new PrintWriter(fout);
			pout.write(text);
			pout.close();
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void removePropertyFromDimension(String dimensionName, String propertyName){
		Collection<Dimension> dimensions = descriptor.getDimensions();
		for (Iterator iterator = dimensions.iterator(); iterator.hasNext();) {
			Dimension dim = (Dimension) iterator.next();
			if(dim.getName().equals(dimensionName)){
				dim.removeProperty(propertyName);
			}
		}
	}
	
	public Collection<Dimension> getDimensions(){
		return descriptor.getDimensions();
	}
	
	public void addPropertyToDimension(String dimensionName, Property property){
		for (Iterator iterator = getDimensions().iterator(); iterator.hasNext();) {
			Dimension dimension = (Dimension) iterator.next();
			if(dimension.getName().equals(dimensionName)){
				dimension.addProperty(property);
			}
		}
	}
	
	public Dimension getDimensionByName(String dimensionName){
		Collection<Dimension> dimensions = this.getDimensions(); 
		for (Iterator iterator = dimensions.iterator(); iterator.hasNext();) {
			Dimension dim = (Dimension) iterator.next();
			if(dim.getName().equals(dimensionName)){
				return dim;
			}
		}
		return null;
	}
	
	public void addDimension(Dimension dimension){
		descriptor.addDimension(dimension);
	}
	
	public Property getPropertyByName(String dimensionName, String propertyName){
		Collection<Dimension> dimensions = this.getDimensions();
		for (Iterator iterator = dimensions.iterator(); iterator.hasNext();) {
			Dimension dim = (Dimension) iterator.next();
			if(dim.getName().equals(dimensionName)){
				return dim.getProperty(propertyName);
			}
		}
		return null;
	}
	
	
	public void removeJoin(String joinName){
		this.descriptor.removeJoin(joinName);
	}
	
	public String getFacttable(){
		return this.descriptor.getFacttable();
	}
	
	public Join getJoinByName(String joinName){
		Collection<Join> joins = this.descriptor.getJoins();
		for (Iterator iterator = joins.iterator(); iterator.hasNext();) {
			Join join = (Join) iterator.next();
			if(join.getName().equals(joinName)){
				return join;
			}
		}
		return null;
	}
	
}
